package JackSu.多线程;

public class _06线程间的通信 {

    /**
     * wait(),notify(),notifyalll()都需要在同步代码块或方法中使用
     */

    public static void main(String[] args) {
        Number number = new Number();
        Thread t1 = new Thread(number);
        Thread t2 = new Thread(number);
        t1.setName("线程1");
        t2.setName("线程2");
        t1.start();
        t2.start();
    }
}

class Number extends Thread {

    private int number = 1;

    @Override
    public void run() {
        while (true) {

            synchronized (this) {
                //释放当前阻塞的线程
                notify();
                if (number <= 100) {
                    System.out.println(currentThread().getName() + ":" + number);
                    number++;
                    try {
                        //让当前线程进入阻塞状态
                        wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    break;
                }
            }
        }

    }

}

